%Extrae carateristicas
% f soon las caracteristicas
% fn los nombre 
% d la clase que pertenece.
function [f fn d] = ExtraeCaracteristicas()
clc; close all;
fiError = fopen('error_caracteristicas.txt','w');
cd images
%a = ls('*flor*.bmp');
!ls *.bmp > imgnames.txt
fid = fopen('imgnames.txt','r');
fn = []; % Nombre
fs = []; % Caracteristicas
ok = 1 ;
i=0;
tic;
warning off all;
imgres =  [64 64];
f = zeros(225,1643);
%d = zeros(256,3);
while(ok)
    fn1 = []; % Nombre
    fs1 = []; % Caracteristicas
    fn2 = []; % Nombre
    fs2 = []; % Caracteristicas
    s = fscanf(fid,'%s\n',[1 1]);
    i =i+1;
    if isempty(s)
        ok = 0;
    else
        disp(sprintf('\n\nProcesando Imagen %s',s));
        I = imread(s);
        I = double(I);
        Img = imresize(I,imgres);
        %Img =I;
        [R,E,J] = segbalu(Img);
        try
            [fn1,fs1]= extraeCaracteristicasGeometricas(R); 
             disp(sprintf('\n\n GEO  %f...',toc));
        catch e
            fn1 = zeros(54,22);
            fs1 = zeros(1,54);
          
            disp(e);
            disp(sprintf('\n\n ERROR GEOMETRICAS IMAGEN %s...',s));
            fprintf(fiError, 'ERROR GEOMETRICAS IMAGEN %s %\n',s ,e.message);
            %cd ..
        end
        try
            [fn2,fs2] = extraeCaracteristicasColores(Img,R);
             disp(sprintf('\n\n INTENSIDAD %f...',toc));
        catch e
            disp(e);
           
            fn2 = zeros(1589,22);
            fs2 = zeros(1,1589);
            disp(sprintf('\n\n ERROR INTENSIDAD IMAGEN %s...',s));
            fprintf(fiError, 'ERROR INTENSIDAD IMAGEN %s %s\n',s ,e.message);
        end
         fn = [fn1; fn2];
         fs = [fs1 fs2];
         f(i,:) = fs;
         
         
        [a b c]= extraeClase(s);
        d(i,:) = [a b c];
         disp(sprintf('\n\n PORCENTAJE AVANCE %f...',100*i/226));
    end
    
end
ftoc = toc;
st = fclose(fid);
cd ..
st = fclose(fiError);
save caracteristicas f fn ftoc d 
disp('FINALIZO');
clear;

end 



%
%Metodo que retorna 
% c = 1 si es Caliz, C=2 si es flor
% d = 1 si es una ranado bueno;d = 2 si es arandano malo
% d1 = 1 si es una ranado buen;d1 = 2 arandano desidratado, d1 = 3 hongo,
% d1 = 4 falla mecanica
function [c d d1] = extraeClase(nombre)

%nombreaux =sprintf('%s', nombre);
%%
%nombre = 'flor_b_1_01.bmp';
%nombre = 'flor_mc_1_01.bmp';
%nombre = 'flor_h_1_01.bmp';
%nombre = 'flor_ds_1_01.bmp';
%nombre = 'caliz_b_1_01.bmp';
%nombre = 'caliz_mc_1_01.bmp';
%nombre = 'caliz_h_1_01.bmp';
%nombre = 'caliz_ds_1_01.bmp';

disp(nombre);

for i=0:1
   [str, nombre] = strtok(nombre, '_');

   switch i
       case 0 % caliz o flor
           if(str(1) == 'c')
               c = 1;
           else 
               c = 2;
           end
            
       case 1 % tipo de daño

       %strcmp
       if(str == 'b')
           d = 1;
           d1 = 1;
       else
           d = 2;
           switch str
               case 'ds'
                   d1 = 2;
               case 'h'
                   d1 = 3;
               case 'mc'
                   d1 = 2;
           end
       end
   end
   
end


end

function [fn,fs] = extraeCaracteristicasGeometricas(R)
    
    fn = []; % Nombre
    fs = []; % Caracteristicas
   
    FeatureGeo = [];
    fg = ones(1,4);
    [NameGeo,Feature,UnitGeo] = geofeatures(R,fg);
    FeatureGeo = [FeatureGeo; Feature];
    fn = [fn; NameGeo];
    fs = [fs Feature];
    return 
end

function [fn,fs] = extraeCaracteristicasColores(I,R)
fn = []; % Nombre
fs = []; % Caracteristicas
XRGB = I;
XHSV = rgb2hsv(I);


%%labparameters();
%%load LAB
%%XLAB = rgb2lab(I,M);
costr = ['Gray '; 'Red  '; 'Green'; 'Blue '; 'Hue  '; 'Sat  '; 'Value'; 'L    '; 'a    '; 'b    ' ];
fi=ones(1,6);
%fi=[0,0,0,0,0,1];
warning off
neigh = 0;
for i=1:10
    switch i
        case 1 % Gray
            X = rgb2gray(I/256)*256;
            [NameGray,Feature,UnitGray] = intfeatures(X,double(R),fi,neigh,costr(i,:));

            fn = [fn; NameGray];
            fs = [fs Feature];
        case 2 % Red
            X = XRGB(:,:,1);
            [NameRed,Feature,UnitRed] = intfeatures(X,double(R),fi,neigh,costr(i,:));

            fn = [fn; NameRed];
            fs = [fs Feature];
        case 3 % Green
            X = XRGB(:,:,2);
            [NameGreen,Feature,UnitGreen] = intfeatures(X,double(R),fi,neigh,costr(i,:));
            fn = [fn; NameGreen];
            fs = [fs Feature];
        case 4 % Blue
            X = XRGB(:,:,3);
            [NameBlue,Feature,UnitBlue] = intfeatures(X,double(R),fi,neigh,costr(i,:));

            fn = [fn; NameBlue];
            fs = [fs Feature];
        case 5 % H
            X = XHSV(:,:,1);
            [NameHue,Feature,UnitHue] = intfeatures(X,double(R),fi,neigh,costr(i,:));

            fn = [fn; NameHue];
            fs = [fs Feature];
        case 6 % S
            X = XHSV(:,:,2);
            [NameSat,Feature,UnitSat] = intfeatures(X,double(R),fi,neigh,costr(i,:));

            fn = [fn; NameSat];
            fs = [fs Feature];
        case 7 % V
            X = XHSV(:,:,3);
            [NameValue,Feature,UnitValue] = intfeatures(X,double(R),fi,neigh,costr(i,:));

            fn = [fn; NameValue];
            fs = [fs Feature];
    %        %             case 8 % L*
    %        X = XLAB(:,:,1);
    %        [NameL,Feature,UnitL] = intfeatures(X,double(R),fi,neigh,costr(i,:));
    %        FeatureL = [FeatureL; Feature];
    %        fn = [fn; NameL];
    %        fs = [fs Feature];
    %        %             case 9 % a*
    %        X = XLAB(:,:,2);
    %        [Namea,Feature,Unita] = intfeatures(X,double(R),fi,neigh,costr(i,:));
    %        Featurea = [Featurea; Feature];
    %        fn = [fn; Namea];
    %        fs = [fs Feature];
    %        %           case 10 % b*
    %        X = XLAB(:,:,3);
    %        [Nameb,Feature,Unitb] = intfeatures(X,double(R),fi,neigh,costr(i,:));
    %        Featureb = [Featureb; Feature];
    %        fn = [fn; Nameb];
    %        fs = [fs Feature];
  
    end
    end
return
end


